package com.tadpole.music.view.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.me.ZhenTiWork1Adapter;
import com.tadpole.music.bean.me.ZhenTiStudentErrorBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.me.ZhenTiStudentErrorIView;
import com.tadpole.music.presenter.me.ZhenTiStudentErrorPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;

import java.util.List;

/**
 * 学生 错题  列表
 * Created By 孙岩
 * on 2020/12/29
 */
public class ZhenTiStudentErrorActivity extends BaseActivity implements ZhenTiStudentErrorIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //刷新加载
    private SmartRefreshLayout rl_refresh;
    //recyclerView
    private RecyclerView recyclerView;
    //adapter
    private ZhenTiWork1Adapter zhenTiWork1Adapter;
    //rank_id
    private String rank_id;
    //user_id 学生id
    private String user_id;
    //presenter
    private ZhenTiStudentErrorPresenter zhenTiStudentErrorPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhenti_student_error);
        rank_id = getIntent().getStringExtra("rank_id");
        user_id = getIntent().getStringExtra("user_id");
        initView();
        initListeners();
        zhenTiStudentErrorPresenter = new ZhenTiStudentErrorPresenter();
        zhenTiStudentErrorPresenter.attachView(this);
        zhenTiStudentErrorPresenter.getZhenTiList(false,user_id,rank_id);
    }

    //初始化控件
    private void initView(){
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("实战真题");
        rl_refresh = findViewById(R.id.rl_refresh);
        recyclerView = findViewById(R.id.recyclerView);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ZhenTiStudentErrorActivity.this.finish();
            }
        });

        //添加刷新监听器
        rl_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                zhenTiStudentErrorPresenter.getZhenTiList(true,user_id,rank_id);
            }
        });

        //上拉加载
        rl_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@androidx.annotation.NonNull RefreshLayout refreshLayout) {
                zhenTiStudentErrorPresenter.getZhenTiListMore(user_id,rank_id);
            }
        });
    }

    @Override
    public void showList(final List<ZhenTiStudentErrorBean.DataBeanX.DataBean> list) {
        //列表数据
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(ZhenTiStudentErrorActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager2);
        //创建适配器
        zhenTiWork1Adapter = new ZhenTiWork1Adapter(ZhenTiStudentErrorActivity.this,list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(zhenTiWork1Adapter);
        recyclerView.setFocusable(false);
        zhenTiWork1Adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(ZhenTiStudentErrorActivity.this).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(ZhenTiStudentErrorActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(ZhenTiStudentErrorActivity.this, StudentErrorActivity.class);
                        intent.putExtra("id",list.get(position).getId());
                        intent.putExtra("flag",2);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishLoadMore();
        rl_refresh.finishRefresh();
    }

    @Override
    public void notifyAdapter() {
        zhenTiWork1Adapter.notifyDataSetChanged();
    }


}
